package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattleCampData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangBattleCampDataDaoImpl extends TempDataObjectDao<CrossKuangBattleCampData> {

    public static String getCrossKuangBattleCampDataSql = "select * from `t_c_kuang_battle_camp_data` where `weekId`=? and `groupId`=? ";

    public Map<Integer, CrossKuangBattleCampData> getCrossKuangBattleCampDataMap(int weekId, int groupId) {
        Map<Integer, CrossKuangBattleCampData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangBattleCampDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossKuangBattleCampData parse = parse(rs);
                map.put(parse.getCampId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossKuangBattleCampDataSql = "update `t_c_kuang_battle_camp_data` set serverIdList=?,score=?,totalScore=?,majorUserId=?,minorUserId=?,majorPlaceId=?,minorPlaceId=?,majorMarkTime=?,minorMarkTime=?,updateScoreTime=?,appointTime=? where weekId=? and groupId=? and campId=?";
    @Override
    protected boolean updateData(CrossKuangBattleCampData crossKuangBattleCampData) {
        crossKuangBattleCampData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleCampData.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMajorUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMinorUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getMajorPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getMinorPlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMajorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMinorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getAppointTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getCampId()));
        if (getDb().executeNoneQuery(updateCrossKuangBattleCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossKuangBattleCampDataSql = "insert into `t_c_kuang_battle_camp_data`(weekId,groupId,campId,serverIdList,score,totalScore,majorUserId,minorUserId,majorPlaceId,minorPlaceId,majorMarkTime,minorMarkTime,updateScoreTime,appointTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossKuangBattleCampData crossKuangBattleCampData) {
        crossKuangBattleCampData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattleCampData.getServerIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMajorUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMinorUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getMajorPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattleCampData.getMinorPlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMajorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getMinorMarkTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattleCampData.getAppointTime()));
        if (getDb().executeNoneQuery(insertCrossKuangBattleCampDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossKuangBattleCampData parseData(ResultSet rs) throws SQLException {
        CrossKuangBattleCampData crossKuangBattleCampData = new CrossKuangBattleCampData();
        crossKuangBattleCampData.setWeekId(rs.getInt("weekId"));
        crossKuangBattleCampData.setGroupId(rs.getInt("groupId"));
        crossKuangBattleCampData.setCampId(rs.getInt("campId"));
        String serverIdList = rs.getString("serverIdList");
        if (!StringUtils.isNullOrEmpty(serverIdList)) {
            crossKuangBattleCampData.setServerIdList(JSON.parseObject(serverIdList, new TypeReference<HashSet<Long>>() {
            }));
        }
        crossKuangBattleCampData.setScore(rs.getLong("score"));
        crossKuangBattleCampData.setTotalScore(rs.getLong("totalScore"));
        crossKuangBattleCampData.setMajorUserId(rs.getLong("majorUserId"));
        crossKuangBattleCampData.setMinorUserId(rs.getLong("minorUserId"));
        crossKuangBattleCampData.setMajorPlaceId(rs.getInt("majorPlaceId"));
        crossKuangBattleCampData.setMinorPlaceId(rs.getInt("minorPlaceId"));
        crossKuangBattleCampData.setMajorMarkTime(rs.getLong("majorMarkTime"));
        crossKuangBattleCampData.setMinorMarkTime(rs.getLong("minorMarkTime"));
        crossKuangBattleCampData.setUpdateScoreTime(rs.getLong("updateScoreTime"));
        crossKuangBattleCampData.setAppointTime(rs.getLong("appointTime"));

        return crossKuangBattleCampData;
    }
}
